<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/17
 * Time: 14:23
 */

namespace app\index\controller;


use app\common\Code;
use app\common\Crypt;
use app\common\model\Users;
use app\common\Session;
use think\Controller;
use think\Db;

class User extends Controller
{
    /**
     * 注册
     * 参数：
     * telephone  电话
     * password   密码
     */
    function register()
    {
        $param = input();
        if (
            isset($param["telephone"]) &&
            isset($param["password"])
        ) {
            $user = Users::get_user($param["telephone"]);
            if (!empty($user)) {
                response_json(Code::EXISTS);
            }
            $user_name = time() . rand(1000, 9999);
            Users::register(
                $user_name,
                $param["telephone"],
                Crypt::pwd($param["password"])
            );
            response_json(Code::NONE);
        }
        response_json(Code::FAIL);
    }

    /**
     * 登录
     * 参数：
     * telephone  电话
     * password   密码
     */
    function login()
    {
        $param = input();
        if (
            isset($param["telephone"]) &&
            isset($param["password"])
        ) {
            $user = Users::get_user($param["telephone"]);
            if (empty($user)) {
                response_json(Code::FAIL);
            } else if (Crypt::pwd($param["password"]) != $user->password) {
                response_json(Code::FAIL);
            } else {
                $user = Session::login_success($user);
                response_json(Code::NONE, $user);
            }
        }
        response_json(Code::FAIL);
    }

    /**
     * 手机号自动注册登录
     * 参数
     * telephone
     * telephone_code  验证码
     * @return \think\response\Json
     */
    public function sms_login()
    {
        $param = input();
        $result = $this->validate(
            $param,
            [
                'telephone' => 'require|/^1[34578]{1}\d{9}$/',
                'telephone_code' => 'require|number'
            ]);
        if (true !== $result) {
            response_json(Code::PARAM_ERROR);
        }
        /** @var Users $user */
        $user = Users::get(["telephone" => $param['telephone']]);
        if (empty($user)) {   #注册
            if (telephone_valid_code($param['telephone'], $param['telephone_code']) == false) {
                response_json(Code::PIC_CAPTURE_ERROR);
            }
            $name = time() . rand(1000, 9999);
            Db::startTrans();
            try {
                Users::register(
                    $name,
                    $param["telephone"],
                    ''
                );
                /** @var Users $user */
                $user = Users::get(["telephone" => $param['telephone']]);
                Session::login_success($user);
                // 提交事务
                Db::commit();
                response_json(Code::NONE);
            } catch (\Exception $e) {
                Db::rollback();
                response_json(Code::FAIL);
            }
        } else {    #验证手机验证码是否正确
            if (telephone_valid_code($param['telephone'], $param['telephone_code']) == false) {
                response_json(Code::PIC_CAPTURE_ERROR);
            }
            Session::login_success($user);
            response_json(Code::NONE);
        }
    }


    /**
     * 自动登录
     * 参数
     * token 客户端的校验码
     * @param int $token
     */
    function auto_login($token = 0)
    {
        $user = Users::get(["token" => $token]);
        if (empty($user)) {
            response_json(Code::FAIL);
        } else {
            $user = Session::login_success($user);
            response_json(Code::NONE, $user);
        }
        response_json(Code::FAIL);
    }


    /**
     * 退出登录
     * 无参数
     * 返回   请登录的 code
     */
    function logout()
    {
        /** @var \app\common\model\Users $user */
        $user = Session::get_local_user();
        if (empty($user)) {
            if (input('logout_by_app')) {
                $user->cilent_id = null;
                $user->save();
            }
            response_json(Code::NO_LOGIN);
        }
        Session::logout();
        response_json(Code::NO_LOGIN);
    }


    /**
     * 修改头像
     * @param string $head_img
     */
    public function up_head_img($head_img = '')
    {
        $user = Session::get_local_user();
        if (empty($user)) {
            response_json(Code::NO_LOGIN);
        }
        $user->head_img = $head_img;
        $user->save();
        response_json(Code::NONE);
    }

    /**
     * 更改client_id  返回client_id
     * 修改 client_id
     * @param string $cid
     */
    public function up_client_id($cid = '')
    {
        Users::clear_client_id($cid);
        $user = Session::get_local_user();
        if (empty($user)) {
            response_json(Code::NO_LOGIN);
        }
        $user->client_id = $cid;
        $user->save();
        response_json(Code::NONE, [
            "client_id" => $cid
        ]);
    }

    /**
     * 修改密码
     * @param string $password
     */
    public function up_password($password = '')
    {
        /** @var \app\common\model\Users $user */
        $user = Session::get_local_user();
        if (empty($user)) {
            response_json(Code::NO_LOGIN);
        }
        $user->password = Crypt::pwd($password);
        $user->save();
        response_json(Code::NONE);
    }

    /**
     * 验证手机验证码
     * 参数
     * telephone
     * telephone_code
     */
    public function valid_telephone_code()
    {
        $param = input();
        $result = $this->validate(
            $param,
            [
                'telephone' => 'require|/^1[34578]{1}\d{9}$/',
                'telephone_code' => 'require|number'
            ]);
        if (true !== $result) {
            response_json(Code::PARAM_ERROR);
        }
        if (telephone_valid_code($param['telephone'], $param['telephone_code']) == false) {
            response_json(Code::PIC_CAPTURE_ERROR);
        }
        response_json(Code::NONE);
    }

    /**
     * 修改支付密码
     * @param string $password
     */
    public function up_pay_password($password = '')
    {
        /** @var \app\common\model\Users $user */
        $user = Session::get_local_user();
        if (empty($user)) {
            response_json(Code::NO_LOGIN);
        }
        $fund = $user->getUserFund();
        $fund->password = Crypt::pwd($password);
        $fund->save();
        response_json(Code::NONE);
    }

    /**
     * <span style="color:#F00;">是否设置支付密码</span>
     * 参数
     */
    public function is_set_pay_password()
    {
        /** @var \app\common\model\Users $user */
        $user = Session::get_local_user();
        if (empty($user)) {
            response_json(Code::NO_LOGIN);
        }
        $fund = $user->getUserFund();
        if (empty($fund->password)) {
            response_json(Code::YUE_PWD_ERROR);
        } else {
            response_json(Code::NONE);
        }
    }

}